<template>
  <view class="white">
    <!-- 商品列表 -->
    <view class="goods_list">
      <view @click="gotoDetail(item)"  v-for="(item, index) in goodsList" :key="index">
        <my-goods :goods="item"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        flag: false,
        total: 0,
        goodsList: [],
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        }
      }
    },
    
    onReachBottom() {
      if(this.goodsList.length === this.total) return uni.$showMsg('数据加载完毕')
      if(this.flag) return uni.$showMsg('正在请求中')
      this.queryObj.pagenum++
      this.getGoodList()
    },
    
   async onPullDownRefresh() {
      const mode = 'refesh'
      this.queryObj.pagenum = 1
      await this.getGoodList(mode)
      uni.stopPullDownRefresh()
    },
    
    onLoad(options) {
      this.queryObj.cid = options.cid || ''
      this.queryObj.query = options.query || ''
      this.getGoodList()
    },
    
    methods: {
     async getGoodList(mode = '') {
       this.flag = true
       const {data:{message, meta}} = await uni.$http.get('api/public/v1/goods/search', this.queryObj)
       if(meta.status !== 200) return uni.$showMsg()
       this.goodsList = mode === 'refesh' ? message.goods : [...this.goodsList, ...message.goods]
       this.total = message.total
       this.flag = false
      },
      
      gotoDetail(item) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }
    }
  }
</script>

<style lang="scss">
.white{
  background-color: #ffffff;
}

</style>
